<template>
  <div>
    <div class="pay_box">
      <el-form inline>
        <el-form-item label="药品">
          <el-input class="pay_date" v-model="form.drugName" placeholder="请输入药品"></el-input>
          <!-- <PatientSearch v-model="form.patientId" clearable placeholder="患者姓名" /> -->
        </el-form-item>
        <el-form-item label="统计时间">
          <el-date-picker style="width: 48%" v-model="form.startTime" type="date" clearable value-format="yyyy-MM-dd" placeholder="开始日期" />
          -
          <el-date-picker style="width: 48%" v-model="form.endTime" type="date" clearable value-format="yyyy-MM-dd" placeholder="结束日期" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" circle icon="el-icon-search" @click="getChartTable(null)"></el-button>
          <el-button type="primary" style="float: right" @click="exportExcel()">导出</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="pay_box">
      <table class="table-box">
        <thead>
          <tr>
            <th>诊疗记录编号</th>
            <th>档案号</th>
            <th>患者名</th>
            <th>开单医生</th>
            <th>药品编码</th>
            <th>药品名称</th>
            <th>药品规格</th>
            <th>厂家</th>
            <th>数量</th>
            <th>单位</th>
            <th>金额</th>
          </tr>
        </thead>
        <tbody>
          <tr v-for="(item, index) in tabList" :key="index">
            <td>{{ item.cureCode }}</td>
            <td>{{ item.archivesCode }}</td>
            <td>{{ item.name }}</td>
            <td>{{ item.trueName }}</td>
            <td>{{ item.chineseMedicine }}</td>
            <td>{{ item.drugName }}</td>
            <td>{{ item.specs }}</td>
            <td>{{ item.factory }}</td>
            <td>{{ item.useTotal }}</td>
            <td>{{ item.totalUnit | changeAssist(medicineUnitList) }}</td>
            <td>{{ item.drugMoney }}</td>
          </tr>
        </tbody>
      </table>
      <br />
      <pager v-model="pager" @change-pagenum="getChartTable"></pager>
    </div>
  </div>
</template>
<script>
import PatientSearch from "@/pages/_com/patientSearch";
import deptSearch from "@/pages/_com/deptSearch";
import doctorSearch from "@/pages/_com/doctorSearch";
import Excel from "@/_js/excel.js";
export default {
  components: {
    PatientSearch,
    deptSearch,
    doctorSearch,
  },
  data() {
    return {
      medicineUnitList: [],
      form: {
        //患者id
        drugName: void 0,
        startTime: void 0,
        endTime: void 0,
      },
      pager: { pageSize: 10, pageNum: 1, total: 0 },
      tabList: [],
    };
  },
  mounted() {
    this.getCommonConfigByKey("药品剂量单位").then((res) => {
      this.medicineUnitList = res;
    });
    this.getChartTable(null);
  },
  methods: {
    getChartTable(arg) {
      if (arg === null) {
        this.pager = this.$options.data().pager;
      }
      window._this = this;
      let th = this;
      th.showLoading();
      let j = Object.assign({}, this.form, th.pager);
      th.$api
        .getPrescriptionDrugPrescriptionIncome(j)
        .then((res) => {
          if (res.executed) {
            th.tabList = res.dataBody.list;
            th.pager.total = res.dataBody.total;
            th.showLoading(false);
          } else {
            th.$message.error(res.message);
            th.showLoading(false);
          }
        })
        .catch((error) => {
          th.showLoading(false);
        });
    },
    exportExcel() {
      let json = {
        pageNum: 1,
        pageSize: 10000,
      };
      json = Object.assign(json, this.form);
      this.$api.getPrescriptionDrugPrescriptionIncome(json).then((res) => {
        if (res.executed) {
          let list = res.dataBody.list;
          list.map((res) => {
            let flag = this.medicineUnitList.filter((rs) => rs.id == res.totalUnit);
            res.totalUnitName = flag ? flag[0].desc : "";
          });
          Excel(
            list,
            [
              { key: "cureCode", name: "诊疗记录编号" },
              { key: "archivesCode", name: "档案号" },
              { key: "name", name: "患者名" },
              { key: "trueName", name: "开单医生" },
              { key: "chineseMedicine", name: "药品编码" },
              { key: "drugName", name: "药品名称" },
              { key: "specs", name: "药品规格" },
              { key: "factory", name: "厂家" },
              { key: "useTotal", name: "数量" },
              { key: "totalUnitName", name: "单位" },
              { key: "drugMoney", name: "金额" },
            ],
            "处方收入"
          );
        }
      });
    },
  },
  computed: {
    //剂量单位
    unitEnum() {
      console.log("11", this.$store.state);
      return this.$store.state.doctorMZ.enum.doseUnitEnum;
    },
  },
};
</script>
<style lang="scss" scoped>
.pay_box {
  margin-top: 10px;
  display: inline-block;
  width: 100%;
  overflow-x: auto;
  .table-box {
    min-width: 600px;
    font-size: 12px;
  }
}
.chart_box {
  display: inline-block;
  width: 100%;
  height: 400px;
}
</style>
